package com.ruoyi.mushcoin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.orm.core.page.PageQuery;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.orm.core.service.impl.BaseServiceImpl;
import com.ruoyi.mushcoin.domain.TgLanguageData;
import com.ruoyi.mushcoin.domain.bo.TgLanguageDataBo;
import com.ruoyi.mushcoin.domain.vo.TgLanguageDataVo;
import com.ruoyi.mushcoin.mapper.TgLanguageDataMapper;
import com.ruoyi.mushcoin.service.ITgLanguageDataService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

import static com.ruoyi.mushcoin.domain.table.TgLanguageDataTableDef.TG_LANGUAGE_DATA;

/**
 * TG多国语言Service业务层处理
 *
 * @author billy
 * 2025-03-05
 */
@Service
public class TgLanguageDataServiceImpl extends BaseServiceImpl<TgLanguageDataMapper, TgLanguageData> implements ITgLanguageDataService {
    @Resource
    private TgLanguageDataMapper tgLanguageDataMapper;

    @Override
    public QueryWrapper query() {
        return super.query().from(TG_LANGUAGE_DATA);
    }

    private QueryWrapper buildQueryWrapper(TgLanguageDataBo tgLanguageDataBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(TG_LANGUAGE_DATA.LANGUAGE_TYPE.eq(tgLanguageDataBo.getLanguageType()));
        queryWrapper.and(TG_LANGUAGE_DATA.MODULE_TYPE.eq(tgLanguageDataBo.getModuleType()));
        queryWrapper.and(TG_LANGUAGE_DATA.MODULE_DATA_ID.eq(tgLanguageDataBo.getModuleDataId()));
        queryWrapper.and(TG_LANGUAGE_DATA.VALUE.eq(tgLanguageDataBo.getValue()));

        return queryWrapper;
    }

    /**
     * 查询TG多国语言
     *
     * @param id TG多国语言主键
     * @return TG多国语言
     */
    @Override
    public TgLanguageDataVo selectById(Long id) {
        return this.getOneAs(query().where(TG_LANGUAGE_DATA.ID.eq(id)), TgLanguageDataVo.class);

    }

    /**
     * 查询TG多国语言列表
     *
     * @param tgLanguageDataBo TG多国语言Bo
     * @return TG多国语言集合
     */
    @Override
    public List<TgLanguageDataVo> selectList(TgLanguageDataBo tgLanguageDataBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(tgLanguageDataBo);
        return this.listAs(queryWrapper, TgLanguageDataVo.class);
    }

    /**
     * 分页查询TG多国语言列表
     *
     * @param tgLanguageDataBo TG多国语言Bo
     * @return 分页TG多国语言集合
     */
    @Override
    public TableDataInfo<TgLanguageDataVo> selectPage(TgLanguageDataBo tgLanguageDataBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(tgLanguageDataBo);
        Page<TgLanguageDataVo> page = this.pageAs(PageQuery.build(), queryWrapper, TgLanguageDataVo.class);
        return TableDataInfo.build(page);
    }

    /**
     * 新增TG多国语言
     *
     * @param tgLanguageDataBo TG多国语言Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(TgLanguageDataBo tgLanguageDataBo) {
        TgLanguageData tgLanguageData = MapstructUtils.convert(tgLanguageDataBo, TgLanguageData.class);


        return this.save(tgLanguageData);//使用全局配置的雪花算法主键生成器生成ID值
    }

    /**
     * 新增TG多国语言，前台提供主键值，一般用于导入的场合
     *
     * @param tgLanguageDataBo TG多国语言Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(TgLanguageDataBo tgLanguageDataBo) {
        TgLanguageData tgLanguageData = MapstructUtils.convert(tgLanguageDataBo, TgLanguageData.class);


        return tgLanguageDataMapper.insertWithPk(tgLanguageData) > 0;//前台传来主键值
    }

    /**
     * 修改TG多国语言
     *
     * @param tgLanguageDataBo TG多国语言Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(TgLanguageDataBo tgLanguageDataBo) {
        TgLanguageData tgLanguageData = MapstructUtils.convert(tgLanguageDataBo, TgLanguageData.class);
        if (ObjectUtil.isNotNull(tgLanguageData) && ObjectUtil.isNotNull(tgLanguageData.getId())) {
            boolean updated = this.updateById(tgLanguageData);
            return updated;
        }
        return false;
    }


    /**
     * 批量删除TG多国语言
     *
     * @param ids 需要删除的TG多国语言主键集合
     * @return 结果:true 删除成功，false 删除失败
     */
    @Transactional
    @Override
    public boolean deleteByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

}
